/**
 * Copyright (C) 2009-2010, Magus Tech.Co.,Ltd.
 * 
 * \File: main_entry.c
 * \author: coanor
 * \date: 2010/4/15
 *
 * \brief: sql-parser main entry
 *******************************************************************************/
#include "main/main_entry.h"
#include "nodes/op_list.h"
#include "nodes/parse_nodes.h"
#include "parser/parser.h"
#include "print/print.h"

#ifndef TCOP_DONTUSENEWLINE
 int use_new_line = 1;  /* Use newlines query delimiters (the default) */
#else
 int use_new_line = 0;  /* Use EOF as query delimiters */
#endif /* TCOP_DONTUSENEWLINE */

 bool g_is_empty_query = false;

/**
 * interactive_backend() - 
 * \brief: receive user's sql-command
 * \param: in_buf[in]
 * \return: char
 * \note: only process 'Q' returns currently,
 *        may add more functions for the future.
 */
static char interactive_backend(char *in_buf)
{
    char *stuff = in_buf;
    int c;
    bool end;
    bool backslash_seen  = false; /* see '\' ?*/

    printf("> "); /* promptor */

    for (;;)
    {
        if (use_new_line)
        {
            /* if use '\n' as dilimter... */
            while((c = getc(stdin) != EOF))
            {
                if (c == '\n')
                {
                    if (backslash_seen)
                    {
                      stuff--;
                      continue;
                    }
                    else
                    {
                        *stuff++ = '\n';
                        *stuff++ = '\0';
                        break;
                    }
                }
                else if (c == '\\')
                {
                    backslash_seen = true;
                } 
                else
                {
                    backslash_seen = false;
                }
                *stuff++ = (char)c;
            }

            if (c == EOF)
            {
                end = true;
            }
        }
        else
        {
            /* read all the characters, untuil EOF */
            while ( (c = getc(stdin)) != EOF)
            {
                *stuff++ = (char)c;
            }

            if (stuff == in_buf)
            {
                end = true;
            }
        }

        /* get one input, process it */
        break;
    }

    return 'Q';
}

/**
 * main_entry - 
 * \brief: sql-parser entry
 * \param: argc[in], argv[in]
 * \return: int
 * \note:
 */
int main_entry(int argc, char *argv[])
{
    char parser_input[MAX_PARSE_BUFFER];
    char firstchar;
    
    for (;;) 
    {
        memset(parser_input, 0, MAX_PARSE_BUFFER);

        firstchar = interactive_backend(parser_input);
        
        switch(firstchar)
        {
            case 'Q':
                fflush(stdout);
                
                if (parser_input[0] == ' ' &&
                    parser_input[1] == '\0')
                {
                    /* null input */
                    g_is_empty_query = true;
                } 
                else 
                {
                    g_is_empty_query = false;
                    op_parse(parser_input, (oid_t *) NULL, 0);
                }
                break;
            default:
                elog(WARN, "unknown frontend message was received");
        }
    }   /* for(;;) */
    
    /* should never been here */
    return 1;
}

/**
 * op_parse()
 * \brief: accpet uses sql-input, and parse it
 * \param: query_string[in]
 *         typev[in]
 * \return: void
 * \note
 */
void op_parse(char *query_string, oid_t *typev, int nargs)
{
    query_t *query_tree;
    
    /* transform string to Query */
    query_tree = parser(query_string, typev, nargs);
    
    /* print the query */
    print(query_tree);
}
